-- 添加luan函数用于管理表的大小
local function luan(t, value)
    if not t then t = {} end
    table.insert(t, value)
    if #t > 2 then
        table.remove(t, 1)
    end
    return t
end

local qiedai = fk.CreateSkill{
    name = "steam__qiedai",
    dynamic_desc = function(self, player)
        if player:getMark("steam__qiedai") == 1 then--1时无背水
            return "steam__qiedai_hasbeishui"
        else
            return "steam__qiedai_nobeishui"
        end
    end,
}

Fk:loadTranslationTable{
    ["steam__qiedai"] = "且怠",
    [":steam__qiedai"] = "你受到伤害时，可防止之，然后选择一项：1.弃置两张牌；2.失去1点体力。",
    ["#steam__qiedai"] = "且怠",
    ["#steam__qiedai-choice"] = "且怠：请选择一项",
    ["qiedai_discard"] = "弃置两张牌",
    ["qiedai_lose"] = "失去1点体力",
    ["#steam__qiedai-addbeishui"] = "且怠：请选择背水移动到的技能",
    [":steam__qiedai_nobeishui"] = "你受到伤害时，可防止之，然后选择一项：1.弃置两张牌；2.失去1点体力。",
    [":steam__qiedai_hasbeishui"] = "你受到伤害时，可防止之，然后选择一项：1.弃置两张牌；2.失去1点体力。背水：将此词条移至“孟明视”的一个相邻技能描述中。",
}

qiedai:addEffect(fk.DamageInflicted, {
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(self.name) and data.to == player
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        if not room:askToSkillInvoke(player, {
            skill_name = qiedai.name,
          }) then
            return false
          end
        local ch={
            "qiedai_discard",
            "qiedai_lose",
        }

        if player:getMark("steam__qiedai") == 1 then
      
            table.insert(ch,"mengmingshi_beishui")
        end
        local choice = room:askForChoice(player, ch, self.name, "#steam__qiedai-choice")
        if choice then
            event:setCostData(self,choice)
            return true
        end
        return false
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local choice = event:getCostData(self)
        local record=player:getTableMark("@fenzhou_record")
        record=luan(record,"steam__qiedai")
        room:setPlayerMark(player,"@fenzhou_record",record)
        -- 防止伤害
        data:preventDamage()
        
        -- 执行选择的效果
        --若背水则移除
        if choice == "mengmingshi_beishui" then
            room:setPlayerMark(player,"steam__qiedai",0)
            local ch={"steam__fenzhou","steam__monao"}
            local choice = room:askForChoice(player, ch, self.name, "#steam__qiedai-addbeishui")
            if choice then
                room:setPlayerMark(player,choice,1)
            end
        end

        if choice ~= "qiedai_lose" then
            room:askToDiscard(player, {
                min_num = 2,
                max_num = 2,
                include_equip = true,
                skill_name = self.name,
                cancelable = false,
            })
        end
        if choice ~= "qiedai_discard" then
            room:loseHp(player, 1)
        end
    end,
})

return qiedai 